package com.cheng.activemq.spring.consumer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * Author: cheng
 * Date: 13-12-23
 * Time: 下午6:38
 */
public class SpringConsumer {

    public void receive() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
        JmsTemplate jmsTemplate = (JmsTemplate)applicationContext.getBean("jmsTemplate");
        Destination destination = (Destination)applicationContext.getBean("destination");
        while(true){
            try{
                TextMessage message = (TextMessage)jmsTemplate.receive(destination);
                if(null != message){
                    System.out.println("收到消息：" + message.getText());
                }else{
                    break;
                }
            } catch (JMSException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    public static void main(String[] args) {
        new SpringConsumer().receive();
    }

}
